<?php namespace Summer\Box\Models;

use Model;
use Config;
use RainLab\User\Models\User as UserModel;
use Winter\Storm\Support\Collection;

/**
 * Model
 */
class BoxItemModel extends Model
{
    use \Winter\Storm\Database\Traits\Validation;
    

    /**
     * @var string The database table used by the model.
     */
    public $table = 'summer_box_items';

    /**
     * @var array Validation rules
     */
    public $rules = [
    ];
    public $belongsTo = [
        'target' => ['RainLab\User\Models\User', 'key' => 'user_id','otherKey'=>'id'],
    ];
    /**
     * 用户搜索
     * @param $query
     * @param $field
     * @return bool
     */
    public function scopeTargetUser($query,$field)
    {
        $user=UserModel::where('userphone',$field)->orWhere('username',$field)->first();
        if(!$user){
            return false;
        }else{
            return $query->where('user_id',$user->id);
        }
    }
    public function getTypeOptions()
    {
        $item_type=Config::get('summer.box::item_type');
        return $item_type;
    }
    public function getFromOptions()
    {
        $item_from=Config::get('summer.box::item_from');
        return $item_from;
    }
    public function getStatusSlugOptions()
    {
        $item_status_slug=Config::get('summer.box::item_status_slug');
        $collection = new Collection($item_status_slug);
        return $collection->pluck('title','slug')->toArray();
    }
    public function beforeCreate()
    {
        if($this->status==1){
            $this->status_slug="normal";
        }elseif($this->status==0){
            $this->status_slug="disable";
        }
    }

}
